//
//  YMRefresh.m
//  JiLaiBao
//
//  Created by youmy on 15/10/27.
//  Copyright © 2015年 youmy. All rights reserved.

//  上拉下拉刷新

#import "YMRefresh.h"
#import <MJRefresh/MJRefresh.h>
@interface YMRefresh()
{
    NSArray *idleImages;
    NSArray *pullingImages;
    NSArray *refreshingImages;
}
//下拉刷新出发的block
@property(nonatomic,copy) void(^DropDownRefreshBlock)(void);
//上拉触发block
@property(nonatomic,copy) void(^UpDropRefreshBlock)(void);
@end
@implementation YMRefresh
+ (void)pullDownRefreshWithTableView:(UITableView *)tableView target:(id)target action:(SEL)action
{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:target refreshingAction:action];
    
    // 设置文字
    [header setTitle:@"轻轻下拉,刷新精彩" forState:MJRefreshStateIdle];
    [header setTitle:@"该放手啦,我要刷新啦" forState:MJRefreshStatePulling];
    [header setTitle:@"刷新中..." forState:MJRefreshStateRefreshing];
    
    // 设置字体
    header.stateLabel.font = [UIFont systemFontOfSize:13];
    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:12];
    
    // 设置颜色
    header.stateLabel.textColor = [UIColor lightGrayColor];
    header.lastUpdatedTimeLabel.textColor = [UIColor lightGrayColor];
    
    // 马上进入刷新状态
    [header beginRefreshing];
    
//    // 设置刷新控件
    tableView.mj_header = header;
}

+(void)pullUpRefreshWithTableView:(UITableView *)tableView target:(id)target action:(SEL)action
{
    //    [self example01];
    
    // 添加默认的上拉刷新
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:target refreshingAction:action];
    
    // 设置文字
    [footer setTitle:@"上拉加载更多数据" forState:MJRefreshStateIdle];
    [footer setTitle:@"加载中..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多的数据" forState:MJRefreshStateNoMoreData];
    
    // 设置字体
    footer.stateLabel.font = [UIFont systemFontOfSize:13];
    
    // 设置颜色
    footer.stateLabel.textColor = [UIColor lightGrayColor];
    
    // 设置footer
    tableView.mj_footer = footer;
}

-(instancetype)init{
    if (self = [super init]) {
        //此gif为逐帧动画由多张图片组成(Image(@"Image")写法和[UIImage imageNamed:@"Image"])
        //闲置状态下的gif(就是拖动的时候变化的gif)
//        NSMutableArray *idleImage = [NSMutableArray array];
//        for (NSUInteger i = 1; i<=60; i++) {
//            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd", i]];
//            [idleImage addObject:image];
//        }
//        idleImages = [idleImage mutableCopy];
//        //已经到达偏移量的gif(就是已经到达偏移量的时候的gif)
//        NSMutableArray *pullingImage = [NSMutableArray array];
//        for (NSUInteger i = 1; i<=10; i++) {
//            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd", i]];
//            [pullingImage addObject:image];
//        }
//        pullingImages = [pullingImage mutableCopy];
//        //正在刷新的时候的gif
//        NSMutableArray *refreshingImage = [NSMutableArray array];
//        for (NSUInteger i = 1; i<=3; i++) {
//            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd", i]];
//            [refreshingImage addObject:image];
//        }
//        refreshingImages = [refreshingImage mutableCopy];
    }
    return self;
}

-(void)normalModelRefresh:(UITableView *)tableview refreshType:(RefreshType)refreshType firstRefresh:(BOOL)firstRefresh timeLabHidden:(BOOL)timeLabHidden stateLabHidden:(BOOL)stateLabHidden dropDownBlock:(void (^)(void))dropDownBlock upDropBlock:(void (^)(void))upDropBlock{
    if (refreshType == RefreshTypeDropDown) {
        //只支持下拉
        //将block传入
        self.DropDownRefreshBlock = dropDownBlock;
        //设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dropDownBlockAction)];
        // 设置文字
        [header setTitle:@"轻轻下拉,刷新精彩" forState:MJRefreshStateIdle];
        [header setTitle:@"该放手啦,我要刷新啦" forState:MJRefreshStatePulling];
        [header setTitle:@"刷新中..." forState:MJRefreshStateRefreshing];
        // 设置字体
        header.stateLabel.font = [UIFont systemFontOfSize:13];
        header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:12];
        // 设置颜色
        header.stateLabel.textColor = [UIColor lightGrayColor];
        header.lastUpdatedTimeLabel.textColor = [UIColor lightGrayColor];
        
        // 马上进入刷新状态
        [header beginRefreshing];
        //是否隐藏上次更新的时间
        header.lastUpdatedTimeLabel.hidden = timeLabHidden;
        //是否隐藏刷新状态label
        header.stateLabel.hidden = stateLabHidden;
        //tableView.mj_header接收header
        tableview.mj_header = header;
        //首次进来是否需要刷新
        if (firstRefresh) {
            [tableview.mj_header beginRefreshing];
        }
        //透明度渐变
        tableview.mj_header.automaticallyChangeAlpha = YES;
    }else if (refreshType == RefreshTypeUpDrop) {
        //只支持上拉
        //传入block
        self.UpDropRefreshBlock = upDropBlock;
        //初始化并指定方法
        tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upDropBlockAction)];
    }else if (refreshType == RefreshTypeDouble) {
        //上拉和下拉都持支持
        //下拉
        //将block传入
        self.DropDownRefreshBlock = dropDownBlock;
        //初始化
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dropDownBlockAction)];
        // 设置文字
        [header setTitle:@"轻轻下拉,刷新精彩" forState:MJRefreshStateIdle];
        [header setTitle:@"该放手啦,我要刷新啦" forState:MJRefreshStatePulling];
        [header setTitle:@"刷新中..." forState:MJRefreshStateRefreshing];
        // 设置字体
        header.stateLabel.font = [UIFont systemFontOfSize:13];
        header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:12];
        // 设置颜色
        header.stateLabel.textColor = [UIColor lightGrayColor];
        header.lastUpdatedTimeLabel.textColor = [UIColor lightGrayColor];
        header.lastUpdatedTimeLabel.hidden = timeLabHidden;
        header.stateLabel.hidden = stateLabHidden;
        //tableView.mj_header接收header
        tableview.mj_header = header;
        //首次进来是否需要刷新
        if (firstRefresh) {
            [tableview.mj_header beginRefreshing];
        }
        //透明度渐变
        tableview.mj_header.automaticallyChangeAlpha = YES;
        //上拉
        //将block传入
        self.UpDropRefreshBlock = upDropBlock;
        //初始化并指定方法
        tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upDropBlockAction)];
    }
}
-(void)gifModelRefresh:(UITableView *)tableView refreshType:(RefreshType)refreshType firstRefresh:(BOOL)firstRefresh timeLabHidden:(BOOL)timeLabHidden stateLabHidden:(BOOL)stateLabHidden dropDownBlock:(void (^)(void))dropDownBlock upDropBlock:(void (^)(void))upDropBlock{
    
//    if (refreshType == RefreshTypeDropDown) {
//        //只支持下拉
//        self.DropDownRefreshBlock = dropDownBlock;
//        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(dropDownBlockAction)];
//        [header setImages:idleImages forState:MJRefreshStateIdle];
//        [header setImages:pullingImages forState:MJRefreshStatePulling];
//        [header setImages:refreshingImages forState:MJRefreshStateRefreshing];
//        header.lastUpdatedTimeLabel.hidden = YES;
//        header.lastUpdatedTimeLabel.hidden = timeLabHidden;
//        header.stateLabel.hidden = stateLabHidden;
//        tableView.mj_header = header;
//        if (firstRefresh) {
//            [tableView.mj_header beginRefreshing];
//        }
//    }else if (refreshType == RefreshTypeUpDrop) {
//        //只支持上拉
//        self.UpDropRefreshBlock = upDropBlock;
//        //初始化并指定方法
//        tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upDropBlockAction)];
//    }else if (refreshType == RefreshTypeDouble) {
//        //支持上拉和下拉加载
//        self.DropDownRefreshBlock = dropDownBlock;
//        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(dropDownBlockAction)];
//        [header setImages:idleImages forState:MJRefreshStateIdle];//普通状态
//        [header setImages:pullingImages forState:MJRefreshStatePulling];//拉拽状态
//        [header setImages:refreshingImages forState:MJRefreshStateRefreshing];//开始刷新
//        header.lastUpdatedTimeLabel.hidden = YES;
//        header.lastUpdatedTimeLabel.hidden = timeLabHidden;
//        header.stateLabel.hidden = stateLabHidden;
//        tableView.mj_header = header;
//        if (firstRefresh) {
//            [tableView.mj_header beginRefreshing];
//        }
//        self.UpDropRefreshBlock = upDropBlock;
//        //初始化并指定方法
//        tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upDropBlockAction)];
//    }
}
//下拉时候触发的block
- (void)dropDownBlockAction {
    if (_DropDownRefreshBlock) {
        _DropDownRefreshBlock();
    }
}

//上拉时候触发的block
- (void)upDropBlockAction {
    if (_UpDropRefreshBlock) {
        _UpDropRefreshBlock();
    }
}
@end
